package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Reduce {

    List<Integer> arrayList;

    public Reduce(List<Integer> arrayList) {
        this.arrayList = arrayList;
    }

    public int getMaximum() {
        int max = arrayList.stream().reduce(0,Integer::max);
        return  max;
    }

    public double getMinimum() {
        int min = arrayList.stream().reduce(1,Integer::min);
        return  min;
    }

    public double getAverage() {
        double average=arrayList.stream().collect(Collectors.averagingDouble(Integer::doubleValue));
        return  average;
    }

    public double getOrderedMedian() {
//        if(arrayList.size()%2!=0){
//            arrayList.stream().collect(Collectors.minBy());
//        }
        double num=0;
        if (arrayList.size()%2==0){
            num=(double) (arrayList.get(arrayList.size()/2-1)+arrayList.get(arrayList.size()/2))/2;
        }else {
            System.out.println("aaaa");
            num=arrayList.get(arrayList.size()/2);
        }
        return num;
    }

    public int getFirstEven() {
        Integer reduce =
                arrayList.stream().filter(a -> a % 2 == 0).findFirst().orElse(arrayList.get(arrayList.size()-1));
        return reduce;
    }

    public int getIndexOfFirstEven() {
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i)%2==0){
                return i;
            }

        }
        return 0;
    }

    public boolean isEqual(List<Integer> arrayList) {
        boolean b=true;
        System.out.println("aaaa"+arrayList.size());
        System.out.println("cccc"+this.arrayList.size());
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i)!=this.arrayList.get(i) || arrayList.size()!=this.arrayList.size()){
                b=false;
            }
        }
        return b;
    }

    public Double getMedianInLinkList(SingleLink singleLink) {
        throw new NotImplementedException();
    }

    public int getLastOdd() {
//        Integer reduce =
//                arrayList.stream().filter(a -> a % 2 != 0).findAny().orElse(arrayList.get(arrayList.size()-1));
//        return reduce;
        int num=0;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i)%2!=0){
                num=arrayList.get(i);
            }

        }
        return num;
    }

    public int getIndexOfLastOdd() {
        int num=0;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i)%2!=0){
                num=i;
            }

        }
        return num;
    }
}
